﻿using StoreTools.Online.View;
using System;
using System.Windows;
using System.Windows.Input;

namespace StoreTools
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            windowControl = new WindowControl();
        }

        private WindowControl windowControl;


        /// <summary>
        /// 鼠标左键拖动事件，通过鼠标左键可以拖动窗口
        /// </summary>
        private void WinMove_LeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    this.DragMove();
                }
            }));
        }

        /// <summary>
        /// 关闭主窗口事件
        /// </summary>
        private void Close_Click(object sender, RoutedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate
            {
                this.Close();
            }));
        }

        /// <summary>
        /// 主窗口缩小到任务栏事件
        /// </summary>
        private void Min_Click(object sender, RoutedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate
            {
                this.WindowState = WindowState.Minimized;
            }));
        }

        /// <summary>
        /// 打开SDK编辑器事件
        /// </summary>
        private void SDK_Click(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// 打开批量改名事件
        /// </summary>
        private void Name_Click(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// 打开批量上版事件
        /// </summary>
        private void Online_Click(object sender, RoutedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate
            {
                windowControl.OnlineWindow.Show();
            }));
        }

        /// <summary>
        /// 打开商品仓库事件
        /// </summary>
        private void Warehouse_Click(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// 打开修改属性事件
        /// </summary>
        private void Properties_Click(object sender, RoutedEventArgs e)
        {

        }
    }

    public partial class WindowControl
    {
        private OnlineWindow? onlineWindow;
        public OnlineWindow OnlineWindow
        {
            get { return GetOnlineWindow(); }
        }

        private OnlineWindow GetOnlineWindow()
        {
            if (onlineWindow == null)
            {
                onlineWindow = new OnlineWindow();
                return onlineWindow;
            }
            else
            {
                if (onlineWindow.IsInitialized)
                {
                    onlineWindow.CloseWin();
                    onlineWindow = null;
                    onlineWindow = new OnlineWindow();
                }
                return onlineWindow;
            }
        }

        private LogWindow? logWindow;
        public LogWindow LogWindow
        {
            get { return GetLogWindow(); }
        }

        private LogWindow GetLogWindow()
        {
            if (logWindow == null)
            {
                logWindow = new LogWindow();
                return logWindow;
            }
            else
            {
                if (logWindow.IsInitialized)
                {
                    logWindow.Close();
                    logWindow = null; 
                    logWindow = new LogWindow();
                }
                return logWindow;
            }
        }

    }
}
